var appPath = getAppPath();	

$(function(){
	initJspGrid();
});

// 自定义动画列表初始化
function initJspGrid(){
	$('#priceGrid').datagrid({
	    url:appPath + "/yjxPrice/getPriceInfo.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam(),
	    singleSelect:false,
		checkbox:false,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	        {field:'price_id',title:'价格ID',width:25,align:'center',sortable:false}, 
	        {field:'price_type',title:'价格类型',width:35,align:'center',sortable:true,
	        	formatter:function(value,rowData,rowIndex){
	        		var ret = 'Nan';
	        		switch(value){
	        		case 2: ret = '<font color="#698B22">去水印订单</font>';break;
	        		case 4: ret = '<font color="#7171C6">秒杀订单</font>'; break;
	        		case 8: ret = '<font color="#8E388E">VIP订单</font>'; break;
	        		case 16: ret = '<font color="red">充值订单</font>'; break;
	        		}
	        		return '<span style="color:#B22222;">'+ret+'</span>';
	        	}
	        },
	        {field:'show_name',title:'展示名称',width:25,align:'center',sortable:true},
	        {field:'name',title:'内部名称',width:25,align:'center',sortable:true},
	        {field:'pricing',title:'计价方式',width:40,align:'center',sortable:true,
	        	formatter:function(value,rowData,rowIndex){
	        		switch(value){
	        		case 10:ret = '<span style="color:green">区间计价</span>';break;
	        		case 20:ret = '单价计价';break;
	        		}
	        		return ret;
	        	}
	        },
	        {field:'unit_price',title:'单价',width:40,align:'center',sortable:true,
	        	formatter:function(value,rowData,rowIndex){
	        		return (Number(value||'0')/100)+'元';
	        	}
	        },
	        {field:'unit_pts',title:'积分数',width:40,align:'center',sortable:true,
	        	formatter:function(value,rowData,rowIndex){
	        		return (Number(value||'0')/100)+'';
	        	}
	        },
	        {field:'unit',title:'单位',width:25,align:'center',sortable:false},  
	        {field:'qty',title:'数量',width:25,align:'center',sortable:false},  
	        {field:'state',title:'状态',width:30,align:'center',sortable:true,
	        	formatter:function(value,rowData,rowIndex){
	        		var ret = 'NaN';
	        		switch(value){
	        		case 10: ret = '<span>有效</span>';break;
	        		case 20: ret = '<span style="color:	#EBEBEB;">无效</span>';break;
	        		}
	        		return ret;
	        	}
	        },
	        {field:'desc',title:'描述',width:60,align:'center',sortable:true},
	        {field : 'order',title : '排序',width : 25,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					if(rowData.order>0){
						return '<a href="javascript:void(0)" onclick="orderSort('+rowData.price_id+','+value+')"">'+value+'</a>';
					}else{
						return '<a href="javascript:void(0)" onclick="orderSort('+rowData.price_id+','+0+')" style="color:#436EEE;">0</a>';
					}
				}
			},
	        {field : 'edit',title : '编辑',width : 18,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					return '<a href="javascript:void(0)" style="font-size:14px;" onclick="showDetail('+rowData.price_id+')">编辑</a>';
				}
			},
	        {field:'create_time',title:'创建时间',width:60,align:'center',sortable:true},
	        {field:'update_time',title:'修改时间',width:60,align:'center',sortable:true}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[
		{
			text:'添加价格',
			iconCls:'icon-add',
			handler:function(){
				addPriceInfo();
			}
		},
		{
			text:'删除价格',
			iconCls:'icon-no',
			handler:function(){
				delPriceInfo();
			}
		}]
	    
	});

}

/**显示顺序*/
function orderSort(id,number){
	$("#orPricId").val(id);
	$("#order").val(number);
	$("#pricBtn").show();
	$('#pricOrder').window({
		title:'显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#pricOrder').window('move',{top:20,left:100});
	$('#pricOrder').window('open');
}

function cancelUpOrder(){
	$('#pricOrder').window('close');
}

function doUpOrder(){
	var id = $("#orPricId").val();
	var number = $("#order").val();
	if(!number || number.length == 0 || Number(number)>999 || Number(number)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～999','warning');
		return;
	}
	if(!id ||id.length <= 0){
		$.messager.alert('error','请联系管理员,系统故障','warning');
		return;
	}
	var	 url = appPath +"/yjxPrice/updPriceOrder.action";;
	var sendData={
			"priceDto.price_id":id||'',
			"priceDto.order":number||''
	};
	$.post(url,sendData,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','顺序更新成功！','info',function(){
				$('#pricOrder').window('close');
				doQuery();
			});
		}else {
			$.messager.alert('提示信息','顺序更新失败','warning');
		}
	});
}


/**展示单个信息详情 用于修改
 */
function showDetail(id) {
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#priceId").val(id);
	
	
	// 加载数据
	var url = appPath + "/yjxPrice/getOnePriceInfo.action?priceId="+id
	$.get(url,function(ret) {
		if (ret && ret.length > 0) {
				var info = ret[0];
				$("#editShowName").val(info.show_name);
				$("#editName").val(info.name);
				$("#editUtPrice").val(info.unit_price);
				$("#editPrices").val(info.prices);
				$("#editUnit").val(info.unit);
				$("#editDesc").val(info.desc);
				// 价格类型
				$("#editPriceType").combobox({
					data:[{'id':2,'name':'去水印订单'},{'id':4,'name':'秒杀订单'},{'id':8,'name':'VIP订单'}],
					valueField:'id',
					textField:'name',
					value:info.price_type
				});
				
				if (info.pricing){
					$("#editPrices").attr('disabled',false);
				}
				// 价格类型
				$("#editPricing").combobox({
					data:[{'id':10,'name':'区间计价'},{'id':20,'name':'单价计价'}],
					valueField:'id',
					textField:'name',
					value : info.pricing,
					onSelect:function(sel){
						var id=sel.id;
						if (id == 10) {
							$("#editPrices").attr('disabled',false);
						} else {
							$("#editPrices").attr('disabled',false);
							$("#editPrices").val('');
							
							$("#editPrices").attr('disabled','disabled');
						}
					}
				});
				// 价格类型
				$("#editState").combobox({
					data:[{'id':10,'name':'有效'},{'id':20,'name':'无效'}],
					valueField:'id',
					textField:'name',
					value : info.state
				});
		}
		});
		$("#editBtn").show();
		$('#editDlg').window({
			title : '套餐编辑页面',
			iconCls : 'icon-edit',
			modal : true,
			shadow : true,
			collapsible : false,
			minimizable : false,
			maximizable : false
		});
	
		$('#editDlg').window('move', {
			top : 20,
			left : 20
		});
		$('#editDlg').window('open');
}



function delPriceInfo(){
	var selectItem=$("#priceGrid").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','选择需要删除的套餐','warning');
		return;
	}
	$.messager.confirm("确认", "是否确认删除", function (r) {  
        if (r) { 
			var ids = "";
			$.each(selectItem, function(index, item){
				ids = ids + item.price_id + ",";
			});
			console.log(ids);
			var url = appPath+"/yjxPrice/delPriceInfo.action?ids="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','删除成功','info');
					doQuery();
				} else {
					$.messager.alert('提示信息','删除失败','warning');
				}
			});
        }
	});
}


function getQueryParam(){
	var queryObject = {};
	var findDesc = $("#queryDesc").val();// 用户ID
	var findPriceId=$("#queryPriceId").val();// 价格ID
	var findName = $("#queryName").val();// 套餐名称
	var findState = $("#queryState").combobox('getValue');
	if(findDesc&&findDesc.length>0) queryObject.findDesc = findDesc; 
	if(findName&&findName.length>0) queryObject.findName = findName;
	if(findPriceId&&findPriceId.length>0) queryObject.findPriceId = findPriceId;
	if(findState) queryObject.findState = findState;
	return queryObject;
}

/*  查询数据
 */
function doQuery(){
	$("#priceGrid").datagrid('unselectAll');
	$("#priceGrid").datagrid('load',getQueryParam());
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}

// 新建价格信息
function addPriceInfo(){
	$("#editTable").show();
	$('#editForm').form('clear');
	$('#editDlg').window({
		title : '新建套餐信息',
		iconCls : 'icon-edit',
		modal : true,
		shadow : true,
		collapsible : false,
		minimizable : false,
		maximizable : false
	});
	$("#editPrices").attr('disabled','disabled'); // 区间计价默认为不可选
	// 价格类型
	$("#editPriceType").combobox({
		data:[{'id':2,'name':'去水印订单'},{'id':4,'name':'秒杀订单'},{'id':8,'name':'VIP订单'}],
		valueField:'id',
		textField:'name',
		value:2
	});
	// 价格类型
	$("#editPricing").combobox({
		data:[{'id':10,'name':'区间计价'},{'id':20,'name':'单价计价'}],
		valueField:'id',
		textField:'name',
		value : 20,
		onSelect:function(sel){
			var id=sel.id;
			if (id == 10) {
				$("#editPrices").attr('disabled',false);
			} else {
				$("#editPrices").attr('disabled',false);
				$("#editPrices").val('');
				
				$("#editPrices").attr('disabled','disabled');
			}
		}
	});
	// 价格类型
	$("#editState").combobox({
		data:[{'id':10,'name':'有效'},{'id':20,'name':'无效'}],
		valueField:'id',
		textField:'name',
		value : 10
	});
	
	$('#editDlg').window('move', {
		top : 20,
		left : 20
	});
	$('#editDlg').window('open');
}

//确认提交编辑
function doEdit() {
	var editShowName = $("#editShowName").val();
	var editName = $("#editName").val();
	var editUtPrice = $("#editUtPrice").val();
	var editPrices = $("#editPrices").val();
	var editUnit = $("#editUnit").val();
	var editDesc = $("#editDesc").val();
	var priceId = $("#priceId").val();
	
	var editPriceType = $("#editPriceType").combobox('getValue');
	var editPricing = $("#editPricing").combobox('getValue');
	var editState = $("#editState").combobox('getValue');
	
	if (editShowName) {
		// 判断单价和计量单位是否为空
		if (editUtPrice==null || editUtPrice.length<=0){
			$.messager.alert('提示信息','单价不能为空','warning');
			return;
		}
		if (editUnit==null || editUnit.length<=0){
			$.messager.alert('提示信息','计量单位不能为空','warning');
			return;
		}
		
		var	 url=appPath + "/yjxPrice/editPriceInfo.action";// 新建
		var sendData = {
				"priceDto.price_id" : priceId || "",
				"priceDto.price_type" : editPriceType || "",
				"priceDto.show_name" : editShowName || "",
				"priceDto.name" : editName || "",
				"priceDto.pricing" : editPricing || "",
				"priceDto.unit_price" : editUtPrice || "",
				"priceDto.prices" : editPrices || "",
				"priceDto.unit" : editUnit || "",
				"priceDto.state" : editState || "",
				"priceDto.desc" : editDesc || ""
			};
			$.post(url, sendData, function(data) {
				if (data == "ok") {
					$.messager.alert('提示信息', '提交成功', 'info');
					$('#editDlg').window('close');
					doQuery();
				}else {
					$.messager.alert("提示信息", "提交失败", 'info');
				}
			});	
	}else{
		$.messager.alert('提示信息', '请填写展示名称', 'info');
	}
}

//取消
function cancelEdit(){
	$('#editDlg').window('close');
	$("#editForm").form('clear');
}
